package pl.polidea.android.utils.crypto;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Crypto {

	public static String MD5(String text) {
		try {
			final MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(text.getBytes("UTF-8"));
			final byte[] bytes = digest.digest();
			final StringBuffer buffer = new StringBuffer();
			for (final byte b : bytes) {
				String hex = Integer.toHexString(0xFF & b);
				if (hex.length() == 1) {
					hex = "0" + hex;
				}
				buffer.append(hex);
			}
			return buffer.toString();
		} catch (final NoSuchAlgorithmException e) {
			// MD5 is on board
			return null;
		} catch (UnsupportedEncodingException e) {
			// UTF-8 is supported
			e.printStackTrace();
			return null;
		}
	}
}
